/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uit.tkorg.coauthorgraph.servlet.graph;

import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import uit.tkorg.coauthorgraph.bo.CoAuthorGraphBO;
import uit.tkorg.coauthorgraph.dto.ConfProgCommitteeGraphDTO;

/**
 *
 * @author Tin Huynh
 */
public class CalculateNetworkDistanceServlet extends HttpServlet {
   
   protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         try {
            System.out.println("************************************************************");
            System.out.println("------------Calculate Metrics: #4. The Network Distance -------");
            HashMap distanceWithConfNetworkHashMap = new HashMap();
            CoAuthorGraphBO coAuthorGraphBO = new CoAuthorGraphBO();
            HttpSession session = request.getSession();

            UndirectedSparseGraph paperCoAuthorGraph = (UndirectedSparseGraph) session.getAttribute("coAuthorGraph");
            ArrayList confCommitteeGraphDTOList = (ArrayList) session.getAttribute("confCommitteeGraphDTOList");
            if (paperCoAuthorGraph != null && confCommitteeGraphDTOList != null) {
                for (int i=0; i<confCommitteeGraphDTOList.size(); i++) {
                    ConfProgCommitteeGraphDTO confCommitteeGraphDTO = (ConfProgCommitteeGraphDTO) confCommitteeGraphDTOList.get(i);
                    UndirectedSparseGraph confCommitteeGraph = confCommitteeGraphDTO.getGraph();
                    int numberOfOverlapedNode = coAuthorGraphBO.countOverlapOfTwoGraphOnNodeName(paperCoAuthorGraph, confCommitteeGraph);
                    distanceWithConfNetworkHashMap.put(confCommitteeGraphDTO.getConferenceName(), new Integer(numberOfOverlapedNode));

                }
            }
            
            request.setAttribute("distanceWithConfNetworkHashMap", distanceWithConfNetworkHashMap);
            System.out.println("********************** DONE DONE DONE *************************");
            RequestDispatcher dispatcher = request.getRequestDispatcher("./jsp/MetricsNetworkDistance.jsp");
            dispatcher.forward(request, response);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
